package me.cabbit.digitone.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 * Implements the bottom "Status Bar". Does absolutely nothing other than displaying
 * things (Called from outside, usually)
 * 
 * @author Arkaid
 *
 */
class MainWindowStatus extends JPanel {
	private static final long serialVersionUID = 8646068318731640368L;

	//------------------------------------------------------------------//
	//--------------------------- properties ---------------------------//
	//------------------------------------------------------------------//
	
	/** Window this menu belongs to */
	private MainWindow mainWnd;

	/** Label with the text status */
	private JLabel lblStatus;
	
	/** String for the status */
	private String status;
	
	/** Value for the zoom, in "times" (1.0 = 100%)*/
	private double zoom;
	
	/** 
	 * Progress bar. This should come in handy when generating heavier tones. 
	 * For the love of god, though, be careful with the thread safety 
	 */
	protected JProgressBar progressBar;
	
	//------------------------------------------------------------------//
	//----------------------------- methods ----------------------------//
	//------------------------------------------------------------------//

	/**
	 * Consturctor. Build the status bar
	 * 
	 * @param mainWnd Window the status bar belongs to
	 */
	public MainWindowStatus(MainWindow mainWnd) 
	{
		this.mainWnd = mainWnd;

		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.HORIZONTAL;

		//status text
		c.gridx = 0; c.gridy = 0; c.weightx = 0.20;	
		this.lblStatus = new JLabel();
		this.add(this.lblStatus, c);
		
		//progress bar
		c.gridx = 1; c.gridy = 0; c.weightx = 0.80;		
		this.progressBar = new JProgressBar();
		this.progressBar.setVisible(false);
		this.add(this.progressBar, c);
		
		this.status = mainWnd.strings.get("STATUS_READY");
		this.zoom = 1.0;
		updateLabel();
	}
	
	private void updateLabel()
	{
		String tmp;
		
		tmp = String.format(mainWnd.strings.get("ZOOM_LEVEL"), this.zoom * 100);
		
		this.lblStatus.setText(this.status + "  |  " + tmp);
	}
	
	/**
	 * Sets the text on the status bar
	 * @param text Text to display
	 */
	public void setStatus(String text) 
	{
		this.status = text;
		updateLabel();
	}
	
	/**
	 * Sets the zoom on the status bar
	 * @param zoom level expressed in times (1.0 = 100%)
	 */
	public void setZoom(double zoom) 
	{
		this.zoom = zoom;
		updateLabel();
	}
	/**
	 * Sets the progress on the progress bar. Hitting 100% hides it 
	 * @param value the new progress value
	 */
	public void setProgress(int value) 
	{
		if (value == 100)
			this.progressBar.setVisible(false);
		else if (!this.progressBar.isVisible())
			this.progressBar.setVisible(true);
		
		this.progressBar.setValue(value);
	}
}
